function formatNumber (n) {
  const str = n.toString()
  return str[1] ? str : `0${str}`
}

export function formatTime (date) {
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()

  const hour = date.getHours()
  const minute = date.getMinutes()
  const second = date.getSeconds()

  const t1 = [year, month, day].map(formatNumber).join('/')
  const t2 = [hour, minute, second].map(formatNumber).join(':')

  return `${t1} ${t2}`
}
/**
 * 判断是否是空
 * @param {simple value} val 
 */
export function isBlank(val) {
  if (val == null || typeof val == "undefined" || val == "") {
    return true
  }
  return false
}
/**
 * 判断一个字符串是否有特定的字符结尾
 * @param {字符串} str 
 * @param {是否以此字符结尾} end 
 */
export function endWith (str, end) {
  if (end == null || end == "" || str.length == 0 || end.length > str.length) {
    return false
  } else if (str.substring(str.length - end.length) == end) {
    return true
  } else {
    return false
  }
}
/**
 * 判断一个字符串是否有特定的字符开始
 * @param {字符串} str 
 * @param {是否以此字符开始} start 
 */
export function startWith (str, start) {
  if (start == null || start == "" || str.length == 0 || start.length > str.length)
    return false;
  if (str.substr(0, start.length) == start)
    return true;
  else
    return false;
}
//自定义正则匹配拿到url字段
export const  getArgs = (url) =>{
	var args = [];
	var query = url.split("?")[1];
	// Get query string
  var pairs = query.split("&"); 
  // Break at ampersand
  for(var i = 0; i < pairs.length; i++) {
        var pos = pairs[i].indexOf('=');
        // Look for "name=value"
        if (pos == -1) continue;
        // If not found, skip
        var argname = pairs[i].substring(0,pos);// Extract the name
        var value = pairs[i].substring(pos+1);// Extract the value
        value = decodeURIComponent(value);// Decode it, if needed
        var arg = new Object();
        arg.key = argname;
        arg.value = value;
        args.push(arg);
    }
  return args;// Return the object 
}

export function changeUrlArg(url, arg, val){
  var pattern = arg+'=([^&]*)';
    var replaceText = arg+'='+val;
    if(url.match(pattern)){
      url = url.replace(eval('/('+ arg+'=)([^&]*)/g'), replaceText);
    }
    return url;
}